﻿using System.Collections.Generic;
using Seagull.Configuration;
using Seagull.Controls;
using Seagull.DAL.Stat;

namespace Seagull.Shell.Views
{
    public partial class TrendView : BaseView
    {
        private readonly Dictionary<byte, StatChart> _statCharts = 
            new Dictionary<byte, StatChart>(Config.Instance.UIConfig.MaxChannelSelection);

        public TrendView()
        {
            InitializeComponent();
            _channelSelector.ChannelSelectionChanged += OnChannelSelectionChanged;
            _channelSelector.LoadChannelNamesFromConfig();
        }

        private void OnChannelSelectionChanged(object sender, ChannelSelectionChangedEventArgs e)
        {
            var channelId = e.Channel;
            if(e.IsAdded)
            {
                var statChart = new StatChart()
                {
                    Text = ChannelSelector.GetChannelName(channelId)
                };
                _rowContainer.AddControl(statChart);
                _statCharts.Add(channelId, statChart);
                InitStatChart(channelId, statChart);
            }
            else
            {
                var statChart = _statCharts[channelId];
                _rowContainer.RemoveControl(statChart);
                statChart.Dispose();
                _statCharts.Remove(channelId);
            }
        }

        private void InitStatChart(byte channelId, StatChart statChart)
        {
            statChart.StatisticSource = new StatisticSource(channelId);
            statChart.Reset();
        }
    }
}
